package com.cby.ssm.controller;

import com.cby.ssm.entity.PermissionEntity;
import com.cby.ssm.entity.RoleEntity;
import com.cby.ssm.entity.Role_PermissionEntity;
import com.cby.ssm.service.PermissionService;
import com.cby.ssm.service.RoleService;
import com.cby.ssm.service.Role_PermissionService;
import com.cby.ssm.service.User_RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 13296 on 2017/7/20.
 */
@Controller
public class RoleHandler {
    @Autowired
    RoleService rfun;
    @Autowired
    PermissionService pfun;
    @Autowired
    User_RoleService urfun;
    @Autowired
    Role_PermissionService rpfun;

    @ModelAttribute
    public String roleUtil(){
        return null;
    }

    @RequestMapping(method= RequestMethod.GET,path="/showRoles.do")
    public String showRolesView(Model model, HttpServletRequest request){
        try {
            //查询权限
            List<PermissionEntity> permissionList = pfun.searchAllPermission();
            model.addAttribute("allPermissions", permissionList);
            //查询角色
            List<RoleEntity> roleList = rfun.searchAllRole();
            //为每个角色查询权限
            model.addAttribute("allRoles", roleList);
            Map<String, List<Role_PermissionEntity>> map = new HashMap<>();
            for(RoleEntity r:roleList){
                List<Role_PermissionEntity> permissionListOfRoleId = rpfun.SearchAllPermissionsByRoleId(r.getRoleId());
                //System.out.println("permissionListOfRoleId"+r.getRoleId());
                map.put("permissionListOfRoleId"+r.getRoleId(),permissionListOfRoleId);
            }
            model.addAttribute("map",map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @RequestMapping(method = RequestMethod.POST,path = "/showRoles.do")
    public String showRoles(Model model,HttpServletRequest request){
        return null;
    }

    /**
     * 分配角色
     * @param request
     * @return
     */

    @RequestMapping(method = RequestMethod.GET,path = "/allocateRoles.do")
    public String allcateRolesView(String userId,HttpServletRequest request){
        return null;
    }

    @RequestMapping(method = RequestMethod.POST,path = "/allocateRoles.do")
    public String allcateRoles(String action,String searchKey,String pn,String userId,HttpServletRequest request){
        System.out.println("提交角色！！");
        try {
            //处理乱码
            searchKey = new String(searchKey.getBytes("UTF-8"),"ISO-8859-1");
            //分配角色更新
            urfun.deleteRolesOfUserId(userId);
            String[] values = request.getParameterValues("choose");
            System.out.println("根据userId"+userId+"删除");
            for(int i=0;i<values.length;i++){
                System.out.println(values[i]);
                urfun.addRoleOfUserId(userId,values[i]);
            }

        }catch (NullPointerException e){
            System.out.println("没有被选中的！");
            //System.out.println("redirect:/"+action+".do?pn="+pn+"&searchKey="+searchKey);
            return "redirect:/"+action+".do?pn="+pn+"&searchKey="+searchKey;
        }catch (Exception e){
            e.printStackTrace();
        }
        //System.out.println("redirect:/"+action+".do?pn="+pn+"&searchKey="+searchKey);
        return "redirect:/"+action+".do?pn="+pn+"&searchKey="+searchKey;
    }

    /**
     * 添加角色到角色表
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,path = "/addRole")
    public String addRole(Model model,HttpServletRequest request){
        return null;
    }
}
